package org.chenzh.util.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.chenzh.stockdata.exception.*;


@Service
@Transactional(rollbackFor = BizException.class)
public class OrderedProperties extends java.util.Properties {
	/**
     * 
     */
	private static final long serialVersionUID = 1L;

	ArrayList orderedKeys = new ArrayList();

	public OrderedProperties() {
		super();
	}

	public OrderedProperties(java.util.Properties defaults) {
		super(defaults);
	}

	public synchronized Iterator getKeysIterator() {
		return orderedKeys.iterator();
	}

	public static OrderedProperties load(String name) throws IOException {
		OrderedProperties props = null;
		java.io.InputStream is = OrderedProperties.class
				.getResourceAsStream(name);
		if (is != null) {
			props = new OrderedProperties();
			props.load(is);
			return props;
		} else {
			if (!name.startsWith("/")) {
				return load("/" + name);
			} else {
				throw new IOException("Properties could not be loaded.");
			}
		}
	}

	@Override
	public synchronized Object put(Object key, Object value) {
		Object obj = super.put(key, value);
		orderedKeys.add(key);
		return obj;
	}

	@Override
	public synchronized Object remove(Object key) {
		Object obj = super.remove(key);
		orderedKeys.remove(key);
		return obj;
	}
}
